<script setup lang="ts">
// #ifdef H5
import { apiGetOAAuthBind } from '@/api/wechat';
import wechatOa, { UrlScene } from '@/utils/wechat';
import { onLoad } from '@dcloudio/uni-app';
// #endif

import { ref } from 'vue';

interface UserInfo {
    avatar: string;
    nickname: string;
}

const userInfo = ref<UserInfo>();
const errMsg = ref<string>('正在处理，请稍等...');

// const oaAuthBind = <T,>(data: any): Promise<T> => {
//     return new Promise((resolve, reject) => {
//         uni.request({
//             url: '/api/login/oaAuthBind',
//             method: 'POST',
//             data,
//             success: (res) => {
//                 const result = res.data as {
//                     code: number;
//                     data: T;
//                     msg: string;
//                     show: 0 | 1;
//                 };
//                 if (result.code === 1) {
//                     resolve(result.data);
//                 } else {
//                     uni.showToast({
//                         title: result.msg,
//                         icon: 'error',
//                     });
//                     // 回显错误消息
//                     errMsg.value = result.msg;
//                     reject(result.msg);
//                 }
//             },
//             fail: (err) => {
//                 reject(err);
//             },
//         });
//     });
// };
const oaBindWx = async (options: any = { getUrl: true }) => {
    let data = null;
    const { code, key = '', getUrl } = options;

    if (code) {
        try {
            data = await apiGetOAAuthBind<UserInfo>({
                code,
                key,
            });
            // 回显用户信息
            userInfo.value = data;
        } catch (err) {
            errMsg.value = error;
            console.log('绑定失败', err);
        }
    }
    if (getUrl) {
        await wechatOa.getUrl(UrlScene.BIND_WX);
    }
};

onLoad((options) => {
    if (options) {
        const { code, state, scene } = options;

        if (code && state && scene) {
            wechatOa.setAuthData({
                code,
                scene,
            });
        }

        const authData = wechatOa.getAuthData();
        if (authData.code && authData.scene == UrlScene.BIND_WX) {
            try {
                oaBindWx({ ...options, ...authData });
            } catch (error: any) {
                uni.showToast({
                    title: `绑定失败:${error.message || JSON.stringify(error)}`,
                    icon: 'error',
                });
            } finally {
                wechatOa.setAuthData();
            }
        } else if (options.is_pc && options.key) {
            oaBindWx();
        }
    }
});
</script>

<template>
    <view style="width: 100vw; height: 100vh; background-color: white">
        <view
            v-if="userInfo"
            style="
                display: flex;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                padding: 80px 0;
            "
        >
            <image
                :src="userInfo.avatar || '/static/logo.png'"
                style="width: 70px; height: 70px; border-radius: 100%; background-color: #eee"
                alt=""
            />
            <view style="padding: 0.5rem 0">
                {{ userInfo.nickname || '昵称' }}
            </view>
            <view style="font-size: 14px; color: #777"> 绑定成功 </view>
        </view>
        <view
            v-else
            style="
                display: flex;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                padding: 80px 0;
            "
        >
            {{ errMsg }}
        </view>
    </view>
</template>

<style lang="scss" scoped></style>
